<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <script src="atmosphere_gwt_js/atmosphere_gwt_js.nocache.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript" language="javascript">
          var client=null;
          atmosphereOnLoad = function() {
              try {
                  client = new atmosphere.Client("comet");
                  if (client == null) {
                      alert("Failed to create client");
                      return;
                  }
                  client.setOnConnected(function(hb, id) {
                      var stat = document.getElementById("status");
                      stat.innerHTML = "[connected] id: "+id+" heartbeat: " + hb;
                      setError("");
                  });
                  client.setOnDisconnected(function() {
                    var stat = document.getElementById("status");
                    stat.innerHTML = "[disconnected]";
                  });
                  client.setOnMessage(function(msg) {
                    alert("received: " + msg + "\ntype: " + functionName(msg.constructor));
                  });
                  client.setOnHeartbeat(function() {
                      var hb = document.getElementById("hb");
                      hb.innerHTML = new Date().toLocaleTimeString();
                      setError("");
                  });
                  client.setOnError(function(msg, connected){
                      setError(
                        (connected?"[connected] ": "")
                          + msg + " on: " + new Date().toLocaleTimeString()
                      );
                  });
                  client.start();
              } catch (err) {
                  alert("Got error trying to start: " + err);
              }
          };
          function setError(errMsg) {
              try {
                  var err = document.getElementById("error");
                  err.innerHTML = errMsg;
              } catch (ex) {
                  alert("failure: " + ex);
              }
          }
          function functionName(fn) {
            var name=/\W*function\s+([\w\$]+)\(/.exec(fn);
            if(!name)return 'No name';
            return name[1];
          }
          function doPost(message) {
              try {
                  client.post(message);
              } catch (err) {
                  alert("Failed to post: " + err);
              }
          }
          function doBroadcast(message) {
              try {
                  client.broadcast(message);
              } catch (err) {
                  alert("Failed to post: " + err);
              }
          }
      </script>
  </head>
  <body>
      <h1>Atmosphere GWT Javascript Demo</h1>
      <div>Status: <span id="status"></span></div>
      <div>Last heartbeat: <span id="hb"></span></div>
      <div>Last error: <span id="error"></span></div>
      <button type="button" onclick="doPost('Posted message');">Post</button>
      <button type="button" onclick="doPost([1,2,3]);">Post Array</button>
      <button type="button" onclick="doBroadcast('Broadcasted message');">Broadcast</button>
  </body>
</html>
